#include "timer.h"
/*设置TIMER3 CH1(HALL-A PA6) CH2(HALL-B PA7) CH3(HALL-C PB0)为输入捕获模式，异或逻辑输入*/
void TIMER3_Init(void)
{
    /*打开RCC TIMER3时钟*/
    RCC -> APBENR1 |= 0x00000002;
    /*CH口设置为复用模式*/
    U32SetBitField(GPIOA->MODER,12,13,2);//A6
    U32SetBitField(GPIOA->MODER,14,15,2);//A7
    U32SetBitField(GPIOB->MODER,0,1,2);//B0
    /*设置复用编号*/
    U32SetBitField(GPIOA->AFR[0],24,31,0x11);//A6,A7
    U32SetBitField(GPIOB->AFR[0],0,3,1);//B0
    
    TIM3->CR1 = 0x80;/*使能ARR预装载*/
    TIM3->CR2 = 0x80;/*配置霍尔异或输入模式*/
    TIM3->DIER = 0x00000002;/*使得CC1中断*/
    TIM3->CCER = 0x0000000A;/*使能CC1 双边沿*/
    U32SetBitField(TIM3->CCMR1,4,7,9);/*配置1us输入滤波 T_dts = t_ckint = 64M   IC1F[3:0] = 1001 ->  fSAMPLING=fDTS/8, N=8
                                    fSAMPLING = 64M/8/8 = 1M -> tSAMPLING = 1us*/
    U32SetBitField(TIM3->CCMR1,0,1,1);/*配置CC1为输入捕获模式，IC1来源是TI1*/
    /*配置ARR 和PSC*/
    //目标：使得定时器能检测到低至50rpm的转速，n = 60f/p   n = 60/(TP) n = 60/(6*T60*p) n = 60/(6*T60*2)
    //n = 60/(12*T60) n = 5/T60 5/T60 = 50  1/T60 = 10 T60 = 0.1s =100ms = 100000us
    //也即当电机在最低转速运行时，霍尔换向信号时间间隔是 0.1s 令ARR等于65535 配置PSC使得0.1S内ARR不溢出
    //65535*PSC/64000000 > 0.1  可令PSC为100以上
    TIM3->PSC = 128;//2us CNT ++ 一次   
    TIM3->ARR = 65535;
    TIM3->CNT = 0;
    /*使能计数器*/
    TIM3->CR1 = 0x01;
    /*使能输出比较*/
    TIM3->CCER |= 0x01;	
}

/*设置TIMER16 CH1(PB8) TIMER17 CH1(PB9)为输出比较模式，输出50khz的PWM波，配合外围的RC电路实现DAC转换，供外部读取调试*/
void TIMER16_17_Init(void)
{
    /*打开TIMER16、TIMER17时钟*/
    U32SetBit(RCC->APBENR2,17);
    U32SetBit(RCC->APBENR2,18);
    /*配置CH1 IO口模式为复用*/
    U32SetBitField(GPIOB->MODER,16,17,2);//A8
    U32SetBitField(GPIOB->MODER,18,19,2);//A9
    /*配置IO口速度为高速*/
    U32SetBitField(GPIOB->OSPEEDR,16,17,2);//A8
    U32SetBitField(GPIOB->OSPEEDR,18,19,2);//A9
    /*配置复用号*/
    U32SetBitField(GPIOB->AFR[1],0,3,2);//A8
    U32SetBitField(GPIOB->AFR[1],4,7,2);//A9    

    /*TIMER 16*/
    TIM16->CR1 |= (1<<7);/*使能ARR预装载*/
    U32SetBitField(TIM16->CCMR1,4,6,6);/*PWM Mode 1*/
    TIM16->CCMR1 |= (1<<3);/*使能CCR预装载*/
    U32SetBitField(TIM16->CCMR1,0,1,0);/*CC1 channel is configured as output*/

    TIM16->CCER |= 0x00000001;/*使能OC1*/

    /*配置PSC和ARR，产生50HZ的波形*/
    TIM16->PSC = 0;
    TIM16->ARR = 1280 - 1;/*64000000/1280 = 50000*/
    TIM16->CCR1 = 640;/*初始占空比50，也即3.3/2 = 1.65v*/

    TIM16->AF1 &=~ (1<<0);/*失能刹车输入*/
    TIM16->BDTR |= (1<<15);/*MOE = 1 使能PWM输出*/
    /*使能计数器*/
    TIM16->CR1 |= 0x00000001;

    /*TIMER 17*/
    TIM17->CR1 |= (1<<7);/*使能ARR预装载*/
    U32SetBitField(TIM17->CCMR1,4,6,6);/*PWM Mode 1*/
    TIM17->CCMR1 |= (1<<3);/*使能CCR预装载*/
    U32SetBitField(TIM17->CCMR1,0,1,0);/*CC1 channel is configured as output*/

    TIM17->CCER |= 0x00000001;/*使能OC1*/

    /*配置PSC和ARR，产生50HZ的波形*/
    TIM17->PSC = 0;
    TIM17->ARR = 1280 - 1;/*64000000/1280 = 50000*/
    TIM17->CCR1 = 640;/*初始占空比50，也即3.3/2 = 1.65v*/    

    TIM17->AF1 &=~ (1<<0);/*失能刹车输入*/    
    TIM17->BDTR |= (1<<15);/*MOE = 1 使能PWM输出*/
    /*使能计数器*/
    TIM17->CR1 |= 0x00000001;
}


// void TIM14_Init(void)
// {
//     RCC->APBENR2 |= (1<<15);
//     //配置PSC、ARR
//     TIM14->PSC = 64000-1;
//     TIM14->ARR = 1000;
//     TIM14->CNT = 0;
//     //清除溢出标志位
//     TIM14 -> SR &=~ (1<<0);
//     //使能溢出中断
//     TIM14 -> DIER |= (1<<0);
//     //先关闭CNT
//     // TIM14 -> CR1 &=~ (1<<0);
//     TIM14 -> CR1 |= (1<<0);
//     //使能NVIC
//     NVIC_EnableIRQ(TIM14_IRQn);
// 	//return TRUE;
// }

// void TIM3_IRQHandler(void)
// {
//     xBldcRunMode.u16HallVal = BldcGetHallValue();
//     xBldcRunMode.ePhase = BldcHallValToPhase(xBldcRunMode.u16HallVal,xMotorMode.dir);
//     if( PHASE_ERROR != xBldcRunMode.ePhase ){
//         BldcPwmControl(xBldcRunMode.ePhase);                
//     }

//     if( eMotorGetState()== MotorSelfStart )
//     {
//         xMotorMode.selfStartSwitchCnt++;
//         setMotorStartCnt(0);
//     }

//     TIM3 -> SR = 0;
// }
